/*----------------------------------------------------------------------------\
|                                XML构造JS                                     |
|-----------------------------------------------------------------------------|
|                   Copyright (c) 2003 Gnct
|               作者:luoshiqiang(email:Imlsq@hotmail.com)                      |
|-----------------------------------------------------------------------------|
|这个JS主要作用是完成表单打包成XML的程序,需要micorsoft的MSXML包的支持                |
|打包格式按照GNCT weboa的XML文件格式,目前没有定义DTD格式                            |
\----------------------------------------------------------------------------*/


/**
* 定义XML header 字段
*/
var header={
    version  : '1.0',
    purpose  : '',
    module   : '',
    created  : '',
    description : ''
};

/**
*
*/
function XMLCreator()
{
  this.xmlDoc=new ActiveXObject(getDomDocumentPrefix() + ".DOMDocument");
  //this.xmlDoc.async=0;
  var xmlstr="<?xml version=\"1.0\" encoding=\"UTF-8\" ?><gnct></gnct>";
  this.xmlDoc.loadXML(xmlstr);
}

/**
* 取得xml根节点
*/
XMLCreator.prototype.getRoot=function()
{
  return this.xmlDoc.documentElement;
}

/**
* 创建header
*/
XMLCreator.prototype.createHeader=function(header)
{
  var queryString="//gnct";
  var node=this.xmlDoc.documentElement.selectSingleNode(queryString);

  var node1=this.xmlDoc.createElement("header");
  node.appendChild(node1);

  node=node1;

  node1=this.xmlDoc.createElement("version");
  node1.text=header.version;
  node.appendChild(node1);

  node1=this.xmlDoc.createElement("purpose");
  node1.text=header.purpose;
  node.appendChild(node1);

  node1=this.xmlDoc.createElement("module");
  node1.text=header.module;
  node.appendChild(node1);

  node1=this.xmlDoc.createElement("created");
  node1.text=header.created;
  node.appendChild(node1);

  node1=this.xmlDoc.createElement("description");
  node1.text=header.description;
  node.appendChild(node1);
}

/**
* 建立rs节点
*/
XMLCreator.prototype.createFieldResult=function(id)
{
  var queryString="//gnct";
  var node=this.xmlDoc.documentElement.selectSingleNode(queryString);

  var node1=this.xmlDoc.createElement("rs");
  node.appendChild(node1);

  var namedNodeMap =node1.attributes;

  var rs_ID = this.xmlDoc.createAttribute("id");
  rs_ID.text =id;
  namedNodeMap.setNamedItem(rs_ID);

  return node1;
}

/**
* 建立字段
*/
XMLCreator.prototype.appendField=function(rs_id,fieldName,datatype,value,alias)
{
  var queryString="//gnct/rs[@id='"+rs_id+"']";
  var node=this.xmlDoc.documentElement.selectSingleNode(queryString);

  var node1=this.xmlDoc.createElement("field");
  node.appendChild(node1);

  var namedNodeMap =node1.attributes;

  var field_Name = this.xmlDoc.createAttribute("name");
  field_Name.text =fieldName;
  namedNodeMap.setNamedItem(field_Name);

  var field_datatype = this.xmlDoc.createAttribute("datatype");
  field_datatype.text =datatype;
  namedNodeMap.setNamedItem(field_datatype);

  var field_value = this.xmlDoc.createAttribute("value");
  field_value.text =value;
  namedNodeMap.setNamedItem(field_value);

  field_value = this.xmlDoc.createAttribute("alias");
  field_value.text =alias;
  namedNodeMap.setNamedItem(field_value);
  return node1;
}

/**
*return:xml字符串
*/
XMLCreator.prototype.xml=function()
{
   //return this.xmlDoc.xml;
    return this.xmlDoc.documentElement.xml;
}

// used to find the Automation server name
function getDomDocumentPrefix() {
	if (getDomDocumentPrefix.prefix)
		return getDomDocumentPrefix.prefix;

	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".DomDocument");
			return getDomDocumentPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}

	throw new Error("Could not find an installed XML parser");
}